-module(rtps_spdp_eh).

%%% @doc The Simple Participant Discovery Protocol (SEDP) event
%%% manager is triggered on detecting or receiving a change in one of
%%% the remote participants. The change is forwarded to the SPDP
%%% process for further processing.

-behaviour(gen_event).

%% -include_lib("kernel/include/logger.hrl").
-include("rtps.hrl").

%% API

%% gen_event callbacks
-export([init/1, handle_event/2, handle_call/2, 
	 handle_info/2, terminate/2, code_change/3]).

-record(data, {pid :: pid()}).

%%%===================================================================
%%% API
%%%===================================================================

%%%===================================================================
%%% gen_event callbacks
%%%===================================================================

%% @private
init([Pid | _Args]) ->
    {ok, #data{pid = Pid}}.

%% @private

%% Ignore events triggered from adding changes by the process that
%% installed the event handler itself, i.e. the SPDP process installs
%% this handler so it is notified when a new remote participant
%% deposits a new chane in the cache, but it does also write changes
%% to the cache itself which should be ignored by the handler.
handle_event({add_change, {Pid, _Tag}, _Changes}, #data{pid = Pid} = Data) ->
    {ok, Data};
handle_event({add_change, _From, Changes}, #data{pid = Pid} = Data) ->
    [rtps_spdp:change(Pid, Change) || Change <- Changes],
    {ok, Data};
handle_event(_Event, Data) ->
    {ok, Data}.

%% @private
handle_call(_Request, Data) ->
    Reply = ok,
    {ok, Reply, Data}.

%% @private
handle_info(_Info, Data) ->
    {ok, Data}.

%% @private
terminate(_Reason, _Data) ->
    ok.

%% @private
code_change(_OldVsn, Data, _Extra) ->
    {ok, Data}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

